package common.web;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class WebHelper {
	private HttpServletRequest request;
	private HttpSession session;
	private HttpServletResponse response;
	private ServletContext servletContext;
	
	private String contextPath;
	private String realPath;
	
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpSession getSession() {
		return session;
	}
	public void setSession(HttpSession session) {
		this.session = session;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	public ServletContext getServletContext() {
		return servletContext;
	}
	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	
	public String getContextPath() {
		return contextPath;
	}
	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}
	public String getRealPath() {
		return realPath;
	}
	public void setRealPath(String realPath) {
		this.realPath = realPath;
	}
	
	
	public Map<Object, Object> getCookies(HttpServletRequest request) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				map.put(cookies[i].getName(), cookies[i]);
			}
		}

		return map;
	}
	
	public String getUrl(HttpServletRequest request) {
		String context = request.getContextPath();
		String url = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + context;
		return url;
	}
	
	public Map<String, String> getEnumeration(HttpServletRequest request) {
		Map<String, String> map = new HashMap<String, String>();
		Enumeration<?> enumer = request.getParameterNames();
		while (enumer.hasMoreElements()) {
			String paramName = (String) enumer.nextElement();
			String paramValue = request.getParameter(paramName);
			map.put(paramName.trim(), paramValue.trim());
		}
		return map;
	}
}
